/**
 * 
 */
package com.seguriapp.dao;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.net.Uri;
import android.util.Log;

import com.seguriapp.providers.SeguriappProvider;
import com.seguriapp.util.Constants;

/**
 * @author pjohnson
 * 
 */
public class EntityDao {

	public static final String TABLE_NAME = "Entity";
	public static final String COL_ID = "_id";
	public static final String COL_EID = "idEntity";
	public static final String COL_TITLE = "title";
	public static final String COL_URL = "url";
	public static final String COL_LONG = "longitude";
	public static final String COL_ALT = "altitude";
	public static final String COL_LAT = "latitude";
	public static final String COL_TYPE = "type";

	// Content Provider constants
	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ SeguriappProvider.AUTHORITY + "/" + TABLE_NAME);
	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.seguriapp.entities";

	// Create the table
	protected static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + COL_ID + " " + Constants.INTEGER
			+ " PRIMARY KEY," + COL_EID + " " + Constants.INTEGER + ", "
			+ COL_TITLE + " " + Constants.TEXT + ", " + COL_URL + " "
			+ Constants.TEXT + ", " + COL_TYPE + " " + Constants.TEXT + ", "
			+ COL_LONG + " " + Constants.REAL + ", " + COL_ALT + " "
			+ Constants.REAL + ", " + COL_LAT + " " + Constants.REAL + ")";

	public static ContentValues contentValuesToInsert(final JSONObject jSon) {
		final ContentValues values = new ContentValues();
		try {
			values.put(COL_EID, jSon.getInt("id"));
			values.put(COL_TITLE, jSon.getString("title"));
			values.put(COL_URL, jSon.getString("url"));
			values.put(COL_LONG, jSon.getDouble("longitude") * 1E6);
			values.put(COL_LAT, jSon.getDouble("latitude") * 1E6);
			values.put(COL_TYPE, jSon.getJSONObject("type").getInt("id"));
		} catch (JSONException e) {
			Log.e("ErrorJson", e.getMessage());
		}

		return values;
	}
}
